package com.puntodonna.fogliocassa.client.widget;

import java.util.Comparator;
import java.util.List;

import com.google.gwt.cell.client.CheckboxCell;
import com.google.gwt.cell.client.TextCell;
import com.google.gwt.dom.client.Style.Unit;
import com.google.gwt.i18n.client.DateTimeFormat;
import com.google.gwt.i18n.client.NumberFormat;
import com.google.gwt.safehtml.shared.SafeHtmlUtils;
import com.google.gwt.user.cellview.client.Column;
import com.google.gwt.user.cellview.client.Header;
import com.google.gwt.user.cellview.client.SafeHtmlHeader;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.view.client.DefaultSelectionEventManager;
import com.google.gwt.view.client.ListDataProvider;
import com.google.gwt.view.client.ProvidesKey;
import com.google.gwt.view.client.SingleSelectionModel;
import com.puntodonna.fogliocassa.shared.MovimentoObj;

public class MovimentiDataTable extends AbstractDataTableWithPager<MovimentoObj> {

	public static final ProvidesKey<MovimentoObj> KEY_PROVIDER = new ProvidesKey<MovimentoObj>() {
		public Object getKey(MovimentoObj item) {
			return item == null ? null : item.getId();
		}
	};

	public MovimentiDataTable() {
		super(new ListDataProvider<MovimentoObj>(KEY_PROVIDER));
		setSelectionModel(new SingleSelectionModel<MovimentoObj>(KEY_PROVIDER), DefaultSelectionEventManager.<MovimentoObj> createCheckboxManager());
		setSize("100%", "100%");
		setEmptyTableWidget(new Label("Nessun Movimento da visualizzare!"));
	}

	@SuppressWarnings("unchecked")
	public SingleSelectionModel<MovimentoObj> getSingleSelectionModel() {
		return (SingleSelectionModel<MovimentoObj>) getSelectionModel();
	}

	@Override
	protected void initializeColumns() {
		setAutoHeaderRefreshDisabled(true);
		final NumberFormat twoDForm = NumberFormat.getDecimalFormat();

		Column<MovimentoObj, Boolean> checkColumn = new Column<MovimentoObj, Boolean>(new CheckboxCell(true, false)) {
			@Override
			public Boolean getValue(MovimentoObj object) {
				return getSelectionModel().isSelected(object);
			}
		};
		addColumn(checkColumn, SafeHtmlUtils.fromSafeConstant("<br/>"));
		setColumnWidth(checkColumn, 8, Unit.PCT);

		// SUM LORDO
		Header<String> lordoSum = new Header<String>(new TextCell()) {
			@Override
			public String getValue() {
				List<MovimentoObj> items = getVisibleItems();

				double sum = 0.00;
				for (MovimentoObj item : items) {
					sum += item.getLordo();
				}
				if (sum != 0)
					sum = Math.round(sum * 100.0) / 100.0;
				return "Σ Lordo: " + twoDForm.format(sum);
			}
		};

		// SUM NETTO
		Header<String> nettoSum = new Header<String>(new TextCell()) {
			@Override
			public String getValue() {
				List<MovimentoObj> items = getVisibleItems();

				double sum = 0.00;
				for (MovimentoObj item : items) {
					sum += item.getNetto();
				}
				if (sum != 0)
					sum = Math.round(sum * 100.0) / 100.0;
				return "Σ Netto: " + twoDForm.format(sum);
			}
		};

		// SUM IVA
		Header<String> ivaSum = new Header<String>(new TextCell()) {
			@Override
			public String getValue() {
				List<MovimentoObj> items = getVisibleItems();

				double sum = 0.00;
				for (MovimentoObj item : items) {
					if (item.getPercIva() != 0)
						sum += item.getIva();
				}
				if (sum != 0)
					sum = Math.round(sum * 100.0) / 100.0;
				return "Σ Iva: " + twoDForm.format(sum);
			}
		};

		// ELEMENT COUNT
		Header<String> elementCount = new Header<String>(new TextCell()) {
			@Override
			public String getValue() {
				List<MovimentoObj> items = getVisibleItems();
				return "Movimenti: " + items.size();
			}
		};
		// DATA
		Column<MovimentoObj, String> dataColumn = new Column<MovimentoObj, String>(new TextCell()) {
			@Override
			public String getValue(MovimentoObj object) {
				if (object.getData() == null)
					return "notSetted";
				return DateTimeFormat.getFormat("dd/MM/yyyy").format(object.getData());
			}
		};
		dataColumn.setSortable(true);
		getSortHandler().setComparator(dataColumn, new Comparator<MovimentoObj>() {
			public int compare(MovimentoObj o1, MovimentoObj o2) {
				return o1.getData().compareTo(o2.getData());
			}
		});
		addColumn(dataColumn, new SafeHtmlHeader(SafeHtmlUtils.fromSafeConstant("DATA")));

		// LORDO
		Column<MovimentoObj, String> lordoColumn = new Column<MovimentoObj, String>(new TextCell()) {
			@Override
			public String getValue(MovimentoObj object) {
				return ((MovimentoObj) object).getLordo().toString();
			}
		};
		lordoColumn.setSortable(true);
		getSortHandler().setComparator(lordoColumn, new Comparator<MovimentoObj>() {
			public int compare(MovimentoObj o1, MovimentoObj o2) {
				return (o1.getLordo().compareTo(o2.getLordo()));
			}
		});
		addColumn(lordoColumn, new SafeHtmlHeader(SafeHtmlUtils.fromSafeConstant("LORDO")), lordoSum);

		// NETTO
		Column<MovimentoObj, String> nettoColumn = new Column<MovimentoObj, String>(new TextCell()) {
			@Override
			public String getValue(MovimentoObj object) {
				return ((MovimentoObj) object).getNetto().toString();
			}
		};
		nettoColumn.setSortable(true);
		getSortHandler().setComparator(nettoColumn, new Comparator<MovimentoObj>() {
			public int compare(MovimentoObj o1, MovimentoObj o2) {
				return (o1.getNetto().compareTo(o2.getNetto()));
			}
		});
		addColumn(nettoColumn, new SafeHtmlHeader(SafeHtmlUtils.fromSafeConstant("NETTO")), nettoSum);

		// %IVA
		Column<MovimentoObj, String> percIVAColumn = new Column<MovimentoObj, String>(new TextCell()) {
			@Override
			public String getValue(MovimentoObj object) {
				return ((MovimentoObj) object).getPercIVA().toString() + "%";
			}
		};
		percIVAColumn.setSortable(true);
		getSortHandler().setComparator(percIVAColumn, new Comparator<MovimentoObj>() {
			public int compare(MovimentoObj o1, MovimentoObj o2) {
				return (o1.getPercIVA().compareTo(o2.getPercIVA()));
			}
		});
		addColumn(percIVAColumn, new SafeHtmlHeader(SafeHtmlUtils.fromSafeConstant("IVA")), ivaSum);

		// CAUSALE
		Column<MovimentoObj, String> noteColumn = new Column<MovimentoObj, String>(new TextCell()) {
			@Override
			public String getValue(MovimentoObj object) {
				return ((MovimentoObj) object).getCausale();
			}
		};
		noteColumn.setSortable(true);
		getSortHandler().setComparator(noteColumn, new Comparator<MovimentoObj>() {
			public int compare(MovimentoObj o1, MovimentoObj o2) {
				return (o1.getCausale().compareTo(o2.getCausale()));
			}
		});
		addColumn(noteColumn, new SafeHtmlHeader(SafeHtmlUtils.fromSafeConstant("CAUSALE")), elementCount);

		getColumnSortList().push(dataColumn);
		
	}
}